package gqz.mvvm.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2018/6/11.
 */

public class SortView extends View {

    private String TAG = "SortView->";

    private Paint paint;

    private int[] array;

    public SortView(Context context) {
        super(context);
        init();
    }

    public SortView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SortView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawPoint(canvas);
    }

    private void drawPoint(Canvas c) {
        float pw = getWidth() / array.length;
        float ph = getHeight() / array.length;
        RectF rect;
        if (array != null && array.length > 0) {
            for (int i = 0; i < array.length; i++) {
                rect = new RectF(pw * array[i] - pw, i * ph,
                        pw * array[i], (i + 1) * ph);
                c.drawCircle(rect.centerX(), rect.centerY(), ph / 2, paint);
            }
        }
    }

    public void setArray(int[] array) {
        this.array = array;
        invalidate();
    }
}
